package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.PhotoAlbumBean;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.view.CircleImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: 社区图片adapter
 * @Description: java类作用描述
 * @Author: ljp
 * @CreateDate: 2018/9/4 14:11
 * @Version: 1.0
 */
public class PictureAdapter extends BaseAdapter<PhotoAlbumBean.PageBean.ListBean>{
    private Context context;
    private  List<PhotoAlbumBean.PageBean.ListBean> tlist;
    int number = 0;
    Thread thread;
    private OnClick onClick;
    private String flag;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Bundle data = msg.getData();
                byte[] bitmap = data.getByteArray("bitmap");
                //String tag = data.getString("tag");
                CircleImageView view = (CircleImageView) msg.obj;
                GlideLoadUtils.getInstance().glideLoad(context,bitmap,view);
                /*GlideApp.with(context)
                        .load(bitmap)
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(view);*/
            }
        }
    };
    private String TAG = "PictureAdapter";

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public PictureAdapter(Context context, List<PhotoAlbumBean.PageBean.ListBean> tlist, int mLayout,String flag) {
        super(context, tlist, mLayout);
        this.context = context;
        this.tlist = tlist;
        this.flag = flag;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        ViewHolder1 viewHolder1 = new ViewHolder1(view);

        return viewHolder1;
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
       if(viewHolder instanceof ViewHolder1){
           if (flag.equals("home")){//他人主页
               ((ViewHolder1) viewHolder).relaAge.setVisibility(View.GONE);
           }else {
               ((ViewHolder1) viewHolder).relaAge.setVisibility(View.VISIBLE);
           }
           //setImage(tlist.get(position),((ViewHolder1) viewHolder).imageIcon);
           if (!tlist.get(position).getAlbumUrl().equals(((ViewHolder1) viewHolder).imageIcon.getTag(R.id.circle_picture))) {
               // 加载图片
               ((ViewHolder1) viewHolder).imageIcon.setTag(R.id.circle_picture,tlist.get(position).getAlbumUrl());
               GlideLoadUtils.getInstance().glideLoad(context,OssConfig.OSS_HTTP+tlist.get(position).getAlbumUrl(),
                       ((ViewHolder1) viewHolder).imageIcon);
           }
           ((ViewHolder1) viewHolder).imageIcon.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   onClick.onSetPictureClick(((ViewHolder1) viewHolder).imageIcon,position);
               }
           });
        }
    }
    private void setImage(String url, View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OssClient.getInstance().init(context).fileDownload(url, new OssClient.OnDownloadFile() {
                    @Override
                    public void onDownFileSuccess(InputStream inputStream) {
                        byte[] bitmap = null;
                        try {
                            bitmap = OssConfig.readInputStreamToByte(inputStream);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        //if(view.getTag() != null && view.getTag().equals(url)) {
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = view;
                        Bundle bundle = new Bundle();
                        //bundle.putString("tag",url);
                        bundle.putByteArray("bitmap", bitmap);
                        message.setData(bundle);
                        handler.sendMessage(message);
                        //  }
                    }

                    @Override
                    public void onDownFileProgress(String progress) {

                    }
                });
            }
        }).start();

    }

    /**
     * 布局1
     */
    class ViewHolder1 extends BaseViewHolder{
        @BindView(R.id.circle_picture)
        CircleImageView imageIcon;
        @BindView(R.id.rela_age)
        RelativeLayout relaAge;
        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public  interface OnClick{
        void onSetPictureClick(View view,int position);
    }
}
